package com.jay.media.music.ui.codec

import android.content.Intent
import android.media.MediaCodecInfo
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jay.base.ui.BaseFragment
import com.jay.codec.hard.CodecUtils
import com.jay.media.kit.LogHelper
import com.jay.media.music.ui.HomeActivity
import com.jay.media.music.ui.R
import com.jay.media.music.ui.sample.AudioActivity
import com.jay.video.camera.ui.CameraActivity

class CodecSettingFragment : BaseFragment(), View.OnClickListener {

    override fun layoutId(): Int = R.layout.fragment_codec_setting

    override fun initView(view: View) {
        val mToolbar = requireView().findViewById<Toolbar>(R.id.toolbar)
        (mActivity as HomeActivity).setupToolbarNavBar(mToolbar)
    }

    override fun onClick(v: View?) {

    }

    class SettingListFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener {
        val TAG: String = LogHelper.makeLogTag(javaClass)

        private var mCodecList: List<MediaCodecInfo> = ArrayList()

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

            findPreference<Preference>(getString(R.string.key_hardware_codec))?.onPreferenceClickListener =
                this
            findPreference<Preference>(getString(R.string.key_audio))?.onPreferenceClickListener =
                this
            findPreference<Preference>(getString(R.string.key_video))?.onPreferenceClickListener =
                this
            findPreference<Preference>(getString(R.string.key_format))?.onPreferenceClickListener =
                this

        }

        private fun showCodecListDialog() {
            LogHelper.d(TAG, "showCodecListDialog")

            mCodecList = CodecUtils.codecInfos().toList()

            val dialogView = layoutInflater.inflate(R.layout.dialog_list, null)
            val dialog = AlertDialog.Builder(requireContext())
                .setTitle("支持的硬件编解码")
                .setView(dialogView)
                .create()
            var recyclerView = dialogView.findViewById<RecyclerView>(R.id.recyclerView)
            recyclerView?.layoutManager = LinearLayoutManager(requireContext())
            recyclerView?.adapter = CodecAdapter()
            dialog.show()
        }

        inner class CodecAdapter : RecyclerView.Adapter<CodecAdapter.CodecHolder>() {

            inner class CodecHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
                var textView = itemView.findViewById<TextView>(R.id.textView)
            }

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CodecHolder {
                val itemView = layoutInflater.inflate(R.layout.item_codec_layout, null, false)
                return CodecHolder(itemView)
            }

            override fun onBindViewHolder(holder: CodecHolder, position: Int) {
                holder.textView.text =
                    (if (mCodecList[position].isEncoder) "编码器" else "解码器") + ": \r\n" + mCodecList[position].name
            }

            override fun getItemCount(): Int {
                return mCodecList.size
            }
        }

        override fun onPreferenceClick(preference: Preference?): Boolean {
            when (preference?.key) {
                getString(R.string.key_hardware_codec) -> {
                    showCodecListDialog()
                }
                getString(R.string.key_audio) -> {
                    startActivity(Intent(requireContext(), AudioActivity::class.java))
                }
                getString(R.string.key_video) -> {
                    startActivity(Intent(requireContext(), CameraActivity::class.java))
                }
                else -> {
                }
            }

            return true
        }
    }
}